package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;

import com.imooc.mall.filter.UserFilter;
import com.imooc.mall.model.vo.CartVo;
import com.imooc.mall.service.CartService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("商品添加")
    public ApiRestResponse add(@RequestParam Integer productId,@RequestParam Integer count){
        List<CartVo> add = cartService.add(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(add);
    }

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("购物车列表")
    public ApiRestResponse List(){
        // 防止横向越权
        List<CartVo> cartList = cartService.list(UserFilter.currentUser.getId());
        return ApiRestResponse.success(cartList);
    }


    @PostMapping("/update")
    @ResponseBody
    @ApiOperation("更新购物车")
    public ApiRestResponse update(@RequestParam Integer productId ,@RequestParam Integer count){
        List<CartVo> update = cartService.update(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(update);
    }

    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation("删除购物车")
    public ApiRestResponse delete(@RequestParam Integer productId){
        // 不能传入userId CartId 删除的时候不需要用到购物车中的其他信息。所有可以不用传参进去
        List<CartVo> delete = cartService.delete(UserFilter.currentUser.getId(), productId);
        return ApiRestResponse.success(delete);

    }

    @PostMapping("/select")
    @ResponseBody
    @ApiOperation("选择购物车")
    public ApiRestResponse select(@RequestParam Integer productId,@RequestParam Integer selected){
        List<CartVo> delete = cartService.selectOrNot(UserFilter.currentUser.getId(), productId,selected);
        return ApiRestResponse.success(delete);
    }


    @PostMapping("/selectAll")
    @ResponseBody
    @ApiOperation("全选择/全不选择购物车的某件商品")
    public ApiRestResponse selectAll(@RequestParam Integer selected){
        List<CartVo> delete = cartService.selectAll(UserFilter.currentUser.getId(),selected);
        return ApiRestResponse.success(delete);
    }
}